package com.miaowang.salad.jobqueue;

import com.alibaba.fastjson.JSON;
import com.miaowang.salad.data.OkHttpResponseData;
import com.miaowang.salad.net.OkHttpUtils;
import com.miaowang.salad.net.UrlUtils;
import com.path.android.jobqueue.Job;
import com.path.android.jobqueue.Params;

import de.greenrobot.event.EventBus;

/**
 *  获取用户当前的任务
 * @author weihe
 *
 */
public class GetCurrentTaskJob extends Job{
	
	private String phoneNum ;

	public GetCurrentTaskJob(String phoneNum) {
		super(new Params(JobQueuePriority.PRIORITY_HIGH).groupBy(JobQueuePriority.JOBS_GROUP_USER).requireNetwork());
		this.phoneNum = phoneNum;
	}

	@Override
	public void onAdded() {
		
	}

	@Override
	protected void onCancel() {
		
	}

	@Override
	public void onRun() throws Throwable {
		String url = UrlUtils.GET_CURRENT_TASK+ "?" +"phone=" + phoneNum;
        String result = OkHttpUtils.get(UrlUtils.GET_CURRENT_TASK);
        OkHttpResponseData responseData = JSON.parseObject(result, OkHttpResponseData.class);
        EventBus.getDefault().post(new JobEvents.JobSuccess<>(JobEvents.GET_CURRENT_TASK,responseData));	
	}
	
	@Override
	protected boolean shouldReRunOnThrowable(Throwable throwable) {
		EventBus.getDefault().post(new JobEvents.JobFailure(JobEvents.GET_CURRENT_TASK));
		return false;
	}

}
